import Vue from "vue";
import VueRouter from "vue-router";
import Index from "../views/Index.vue";

Vue.use(VueRouter);
const routes = [
  {
    path: "/newlistupload",
    name: "newlistUpload",
    component: () => import("../components/newlistUpload.vue"),
  },
  {
    path: "/myspace/mysetting",
    name: "MySetting",
    component: () => import("../views/MySetting.vue"),
  },
  {
    path: "/myspace/myupload",
    name: "MyUpload",
    component: () => import("../views/MyUpload.vue"),
  },
  {
    path: "/myspace/mynews",
    name: "MyNews",
    component: () => import("../views/MyNews.vue"),
  },
  {
    path: "/myspace/myindex",
    name: "MyIndex",
    component: () => import("../components/MyIndex.vue"),
  },
  {
    path: "/chatdetails/:id",
    name: "ChatDetails",
    component: () => import("../views/ChatDetails.vue"),
    meta: {
      needLogin: true, //需要加校检判断的路由
    },
  },
  {
    path: "/wechat",
    name: "WeChat",
    meta: {
      keepAlive: true,
    },
    component: () => import("../views/WeChat.vue"),
  },
  {
    path: "/bestwords",
    name: "BestWords",
    meta: {
      keepAlive: true,
    },
    component: () => import("../views/BestWords.vue"),
  },
  {
    path: "/wordsdetails/:id",
    name: "WordsDetails",
    component: () => import("../views/WordsDetails.vue"),
  },
  {
    path: "/booksdetails/:id",
    name: "BooksDetails",
    component: () => import("../views/BooksDetails.vue"),
  },
  {
    path: "/bookdetails/:id",
    name: "BookDetails",
    component: () => import("../views/BookDetails.vue"),
  },
  {
    path: "/myfooter",
    name: "Myfooter",
    component: () => import("../components/MyFooter.vue"),
  },
  {
    path: "/mybody",
    name: "MyBody",
    meta: {
      keepAlive: true,
    },
    component: () => import("../components/MyBody.vue"),
  },
  {
    path: "/booklist",
    name: "BookList",
    meta: {
      keepAlive: true,
    },
    component: () => import("../components/BookList.vue"),
  },
  {
    path: "/myswiper",
    name: "Myswiper",
    component: () => import("../components/MySwiper.vue"),
  },
  {
    path: "/newlist",
    name: "NewList",
    meta: {
      keepAlive: true,
    },
    component: () => import("../components/NewList.vue"),
  },
  {
    path: "/mylogin",
    name: "MyLogin",
    component: () => import("../components/MyLogin.vue"),
  },
  {
    path: "/myregister",
    name: "MyRegister",
    component: () => import("../components/MyRegister.vue"),
  },
  {
    path: "/myheader",
    name: "MyHeader",
    component: () => import("../components/MyHeader.vue"),
  },
  {
    path: "/",
    name: "Index",
    meta: {
      keepAlive: true,
    },
    component: Index,
  },
  {
    path: "/*",
    name: "NotFound",
    component: () => import("../components/NotFound.vue"),
  },
];
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
// router.beforeEach((to, from, next) => {
//   if (to.path === "/mylogin" || to.path==="/myregiser") {
//     next();
//   } else {
//     let token = localStorage.getItem("Authorization");

//     if (token === null || token === "") {
//       next("/mylogin");
//     } else {
//       next();
//     }
//   }
// });

router.beforeEach(function (to, from, next) {
  if (to.meta.needLogin) {
    //页面是否登录
    if (localStorage.getItem("token")) {
      //本地存储中是否有token(uid)数据
      next(); //表示已经登录
    } else {
      //next可以传递一个路由对象作为参数 表示需要跳转到的页面
      next({
        name: "/login",
      });
    }
  } else {
    //表示不需要登录
    console.log(localStorage.getItem("token"));
    next(); //继续往后走
  }
});

// // 添加请求拦截器，在请求头中加token
// axios.interceptors.request.use(
//   (config) => {
//     if (localStorage.getItem("Authorization")) {
//       config.headers.Authorization = localStorage.getItem("Authorization");
//     }

//     return config;
//   },
//   (error) => {
//     return Promise.reject(error);
//   }
// );

// //响应拦截器
// // 不只是成功状态有响应，不同的状态响应码不同
// request.interceptors.response.use((response)=>{
//   let {data} = response;
//   if(data.code =='3' || data.code =='2'){
//       // 响应失败 统一抛出失败信息
//       return Promise.reject(new Error('error'))
//   }
//   if(data.code =='0'){
//       // token失效
//       console.log('登录失败');
//   }
//   if(data.code == '1'){
//       router.push('/index/home');
//   }
//   return response; //如果不返回，下面接收不到响应体中的内容
// },error=>{
//   Message({
//       message:'访问超时',
//       duration:10000,
//       type:'error'
//   })
//   return Promise.reject(new Error(error))
// })

export default router;
